package com.citycloud.ccuap.tc.admin.entity;

import static org.springframework.data.mybatis.annotations.Id.GenerationType.AUTO;
import static org.springframework.data.mybatis.annotations.Id.GenerationType.ASSIGNATION;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.Objects;

import com.google.gson.Gson;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.springframework.data.annotation.Version;
import org.springframework.data.mybatis.annotations.Entity;
import org.springframework.data.mybatis.annotations.Column;
import org.springframework.data.mybatis.annotations.Condition;
import org.springframework.data.mybatis.annotations.Conditions;
import org.springframework.data.mybatis.annotations.Id;
import org.springframework.data.repository.query.parser.Part;

import com.citycloud.ccuap.framework.flexjson.transformer.annotation.Convertor;
import com.citycloud.ccuap.framework.service.Model;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 应用功能实体对象
 * @author zhusr
 *
 */
@Data
@NoArgsConstructor
@Entity(table = "sys_admin_interface")
public class SysAdminInterface implements Model {
	private static final long serialVersionUID = 1L;
	
	@Id(strategy = AUTO)
	@Column(name = "interface_id")
	@Condition
	private Long interfaceId;
	
	@Column(name = "interface_name")
	@Condition(type = Part.Type.LIKE,likeMatchMode = 3)
	private String interfaceName;
	
	@Column(name = "interface_url")
	@Condition(type = Part.Type.LIKE)
	private String interfaceUrl;
	
	@Column(name = "interface_type")
	@Condition
	private Short interfaceType;
	
	@Column(name = "order_no")
	@Condition
	private Long orderNo;
	
	@Column(name = "log_flag")
	@Condition
	private Short logFlag;
	
	@Version
	@Column(name = "version")
	@Condition
	private Integer version;
	
	@Column(name = "remark")
	@Condition(type = Part.Type.LIKE)
	private String remark;
	
	@Column(name = "creator")
	@Condition(type = Part.Type.LIKE)
	private String creator;
	
	@Column(name = "created_time")
	@Conditions({ 
		@Condition,
		@Condition(type = Part.Type.GREATER_THAN_EQUAL, properties="startCreatedTime"), 
		@Condition(type = Part.Type.LESS_THAN, properties="endCreatedTime") 
	})
	private Date createdTime;
	
	@Column(name = "updator")
	@Condition(type = Part.Type.LIKE)
	private String updator;
	
	@Column(name = "updated_time")
	@Conditions({ 
		@Condition,
		@Condition(type = Part.Type.GREATER_THAN_EQUAL, properties="startUpdatedTime"), 
		@Condition(type = Part.Type.LESS_THAN, properties="endUpdatedTime") 
	})
	private Date updatedTime;

	@Override
	public String toString() {
		Gson gson = new Gson();
		return gson.toJson(this);
	}

	private static String getPostParams(Object clazz) {
		// 遍历属性类、属性值
		Field[] fields = clazz.getClass().getDeclaredFields();

		StringBuilder requestUrl = new StringBuilder();
		try {
			boolean flag = true;
			String property, value;
			for (int i = 0; i < fields.length; i++) {
				Field field = fields[i];
				// 允许访问私有变量
				field.setAccessible(true);

				// 属性名
				property = field.getName();
				// 属性值
				if(Objects.isNull(field.get(clazz))) {
					continue;
				}
				value = field.get(clazz).toString();

				String params = property + "=" + value;
				if (flag) {
					requestUrl.append(params);
					flag = false;
				} else {
					requestUrl.append("&" + params);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return requestUrl.toString();
	}

	public static void main(String[] args){
 		SysAdminInterface sysAdminInterface = new SysAdminInterface();
 		sysAdminInterface.setInterfaceName("测试接口1");
 		sysAdminInterface.setInterfaceUrl("http");
 		sysAdminInterface.setRemark("测试备注");
 		sysAdminInterface.setLogFlag((short) 1);
 		System.out.println(getPostParams(sysAdminInterface));
	}

}